
package com.csz.model;

import com.csz.dao.Persistencia;
import java.io.Serializable;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;


/* @author Arthur F. Zanona */

public class Pais extends Persistencia {
    private static final long serialVersionUID = 1L;

    private String nome;
    private String paiDescricao;

    public Pais() {
    }

    public Pais(Integer paiId) {
        this.id = paiId;
    }

    public Pais(Integer id, String paiNome, String paiDescricao) {
        this.id = id;
        this.nome = paiNome;
        this.paiDescricao = paiDescricao;
    }

   public String getNome() {
        return nome;
    }

    public void setNome(String paiNome) {
        this.nome = paiNome;
    }
    
    public String getPaiDescricao() {
        return paiDescricao;
    }

    public void setPaiDescricao(String paiDescricao) {
        this.paiDescricao = paiDescricao;
    }

    @Override
    public void salvar() {
        String namedQuery = this.getId() == null ? Inserir : Atualizar ;
        //----- params aqui
        List<Object> params = new ArrayList<Object>();
        //---- fim params
        if ( this.getId() != null ) params.add( this.getId() );
        PreparedStatement ps = createStatementNamedQuery(namedQuery, params);
        try {
            ps.execute();
        } catch (SQLException ex) {}
    }

    public static Object consultar(Integer id) {
        return Pais.consultarNamedQuery(ConsultarPorId, Arrays.asList(new Object[]{id}));
    }

    @Override
    public void remover() {
        PreparedStatement ps = createStatementNamedQuery(Remover, Arrays.asList( new Object[] { this.getId() } ));
        try {
            ps.execute();
        } catch (SQLException ex) {}
    }

    public static List<Object> consultar(){
        return Pais.consultarNamedQuery(ConsultarTodos, Arrays.asList(new Object[]{}));
    }

   public static List<Object> consultarNamedQuery(String namedQuery, List params){
        PreparedStatement ps = createStatementNamedQuery( namedQuery, params );
        List<Object> lista = new ArrayList<Object>();
        try {
            ResultSet rs = ps.executeQuery();
            while ( rs.next() ) {
                Pais p = new Pais(
                        rs.getInt("pai_id")  ,
                        rs.getString("pai_nome"),
                        rs.getString("pai_descricao")
                );
                lista.add(p);
            }
        }
        catch (SQLException ex) {}
        return lista;
    }

    // QUERY SQL --------------------------------------------------------------------------------------------------------------
    public static final String ConsultarTodos  = "SELECT pai_id,pai_nome,pai_descricao FROM pais";
    public static final String ConsultarPorId = "SELECT pai_id,pai_nome,pai_descricao FROM pais WHERE pai_id = ?";
    private static final String Remover = "CALL remover_pais(?)";
    private static final String Atualizar = "CALL atualizar_pais(?,?,?,?)";
    private static final String Inserir = "CALL inserir_pais (?,?,?,?,?)";
    // FIM QUERY SQL --------------------------------------------------------------------------------------------------------------


}
